﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace Journal.net
{
    public partial class JournalForm : Form
    {
        string strDate = DateTime.Now.ToLongDateString() ;

        public JournalForm()
        {
            InitializeComponent();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            System.DateTime.TryParseExact (DateTime.Now.ToLongDateString ,
            
            string xmlFile = Application.StartupPath + @"\" + DateTime.Now.ToLongDateString() + ".xml";
            XmlDocument xDoc = new XmlDocument();

            try
                {
                    if (File.Exists(xmlFile))
                    {
                        xDoc.Load(xmlFile);
                    }
                    else
                    {
                        FileStream stream = new FileStream(xmlFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

                        byte[] buffer = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" + " <entries>" + @"</entries>");

                        stream.Write(buffer, 0, buffer.Count());

                        stream.Flush();

                        stream.Close();

                        xDoc.Load(xmlFile);
                    }
                }
                catch (Exception)
                {
                    if (!(File.Exists(xmlFile)))
                    {
                        FileStream stream = new FileStream(xmlFile, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);

                        byte[] buffer = Encoding.UTF8.GetBytes("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\" ?>" + " <entries>" + @"</entries>");

                        stream.Write(buffer, 0, buffer.Count());

                        stream.Flush();

                        stream.Close();
                    }                    
                }         

            try
            {
                XmlElement entry = xDoc.CreateElement("entry");

                XmlElement date = xDoc.CreateElement("date");

                XmlElement time = xDoc.CreateElement("time");

                XmlElement category = xDoc.CreateElement("category");

                XmlElement message = xDoc.CreateElement("message");

                XmlText dateText = xDoc.CreateTextNode(strDate);
                XmlText timeText = xDoc.CreateTextNode(DateTime.Now.ToShortTimeString());
                XmlText categoryText = xDoc.CreateTextNode(CategorytreeView.SelectedNode .Text );
                XmlText messageText = xDoc.CreateTextNode(MessagerichTextBox.Text);

                entry.AppendChild(date);

                entry.AppendChild(time);

                entry.AppendChild(category);

                entry.AppendChild(message);

                date.AppendChild(dateText);

                time.AppendChild(timeText);

                category.AppendChild(categoryText);

                message.AppendChild(messageText);

                xDoc.DocumentElement.AppendChild(entry);

                xDoc.Save(xmlFile);
              
            }
            catch (Exception)
            {
                if (MessagerichTextBox.Text != "")
                {
                    MessageBox.Show("Can not save file this time.");
                }                              
            }                 
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            JournalmonthCalendar.ShowTodayCircle = true;                  
            
        }

        private void JournalmonthCalendar_DateSelected(object sender, DateRangeEventArgs e)
        {
            strDate = e.Start.ToLongDateString ();            
        }

        private void MessagerichTextBox_TextChanged(object sender, EventArgs e)
        {

        }       

        private void btnCategory_Click(object sender, EventArgs e)
        {
            CategoryForm categoryForm = new CategoryForm();

            if (categoryForm.ShowDialog() == DialogResult.OK) 
            {
                string category = categoryForm.CategoryName;

                if (category != string.Empty)
                {                                       
#region using enumerator to find category if not present then add to treeview

                    System.Collections.IEnumerator ie = CategorytreeView.Nodes.GetEnumerator();

                    bool isFound = false;

                    while (ie.MoveNext())
                    {
                        TreeNode t = ie.Current as TreeNode;

                        if (t.Text == category)
                        {
                            isFound = true;
                            ie.Reset();
                            break;
                        }
                    }
                    if (isFound == false)
                    
                        CategorytreeView.Nodes.Add(new TreeNode(category).Name = category);
                    
                    else
                     
                        MessageBox.Show("Category you have entered is already present.");
                   
                    CategorytreeView.Update();
#endregion
                }
            }
        }
    }
}
